package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.cent.sdata.Attr;
import com.xhwl.common.pojo.cent.sdata.ProjectExtra;
import com.xhwl.common.query.cent.sdata.AttrQuery;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.dao.IProjectExtraDao;
import com.xhwl.data.service.IAttrService;
import com.xhwl.data.service.IProjectExtraService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Class
 * @Description 项目拓展信息-【IT运管中心】-属性配置添加
 * @Auther zhouxingxiang
 * @Date 2023/7/3 16:32
 */
@Service
public class ProjectExtraServiceImpl extends ServiceImpl<IProjectExtraDao, ProjectExtra> implements IProjectExtraService {

    @Autowired
    private IAttrService attrService;

    @Autowired
    private IProjectExtraDao projectExtraDao;

    @Autowired
    private IProjectExtraService projectExtraService;

    //项目拓展信息-场景
    private static final String attrSceneCode = "projectExtraInfo";

    @Override
    public List<Attr> getExtraAttr(AttrQuery attrQuery) {
        attrQuery.setAttrSceneCode(attrSceneCode);
        return attrService.list(attrQuery);
    }

    @Override
    public String getExtraInfo(Integer projectId) {
        ProjectExtra projectExtra = projectExtraDao.selectOne(new QueryWrapper<ProjectExtra>().eq("project_id", projectId));
        if (null == projectExtra) {
            return null;
        }
        return projectExtra.getJson();
    }

    @Override
    public void save(String jsonStr, Integer projectId) {
        if (null == projectId || StringUtils.isEmpty(jsonStr)){
            return;
        }
        ProjectExtra projectExtra = projectExtraDao.selectOne(new QueryWrapper<ProjectExtra>().eq("project_id", projectId));
        ProjectExtra newExtra = new ProjectExtra();
        if (null != projectExtra){
            BeanUtils.copyProperties(projectExtra, newExtra);
        }
        newExtra.setProjectId(projectId);
        newExtra.setJson(jsonStr);
        projectExtraService.saveOrUpdate(newExtra);
    }

}
